package fr.ginvest.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.Basic;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.UniqueConstraint;

@Entity
@Table(name = "startup", uniqueConstraints={ @UniqueConstraint (columnNames={"nom", "login"})})
public class Startup implements Serializable {

	private static final long serialVersionUID = 6181241152980448255L;
	public enum Etat{ACTIF, INACTIF}

	@Id
	@GeneratedValue(strategy=GenerationType.AUTO) 
	private int id;
	
	@Basic(optional=false)
	private String login;

	@Basic(optional=false)
	private String motDePasse;
	
	@Basic(optional=false)
	private String nom;
	
	@Column(length=5000)
	private String adresse;
	
	private String telephone;
	
	@Column(length=5000)
	private String description;
	
	private String domaineActivite;
	
	@Temporal(TemporalType.DATE)
	private java.util.Date dateCreation;
	
	@Enumerated(EnumType.STRING)
	@Column(length=7, nullable=false)
	private Etat etat;

	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "startup")
	//@JoinColumn(name="startup_fk",referencedColumnName="id")
	private Set<Participation> participations = new HashSet<Participation>();
	
	@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "startup")
	private Set<Avancement> avancements = new HashSet<Avancement>();
	
	public Startup() {}

	public Startup(String nom, String adresse, String telephone,
			String description, String domaineActivite) {
		super();
		this.nom = nom;
		this.adresse = adresse;
		this.telephone = telephone;
		this.description = description;
		this.domaineActivite = domaineActivite;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + id;
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Startup other = (Startup) obj;
		if (id != other.id)
			return false;
		return true;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getLogin() {
		return login;
	}

	public void setLogin(String login) {
		this.login = login;
	}

	public String getMotDePasse() {
		return motDePasse;
	}

	public void setMotDePasse(String motDePasse) {
		this.motDePasse = motDePasse;
	}

	public String getNom() {
		return nom;
	}

	public void setNom(String nom) {
		this.nom = nom;
	}

	public String getAdresse() {
		return adresse;
	}

	public void setAdresse(String adresse) {
		this.adresse = adresse;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getDomaineActivite() {
		return domaineActivite;
	}

	public void setDomaineActivite(String domaineActivite) {
		this.domaineActivite = domaineActivite;
	}

	public Set<Participation> getParticipations() {
		return participations;
	}

	public void setParticipations(Set<Participation> participations) {
		this.participations = participations;
	}
	
	public void addParticipation(Participation participation) {
		this.participations.add(participation);
	}
	
	public Etat getEtat() {
		return etat;
	}

	public void setEtat(Etat etat) {
		this.etat = etat;
	}
	
	public java.util.Date getDateCreation() {
		return dateCreation;
	}
	
	public void setDateCreation(java.util.Date dateCreation) {
		this.dateCreation = dateCreation;
	}
	
	public Set<Avancement> getAvancements() {
		return avancements;
	}

	public void setAvancements(Set<Avancement> avancements) {
		this.avancements = avancements;
	}

	public boolean matchPassword(String pwd) {
        if (pwd == null || "".equals(pwd))
            return false;
        
        if (pwd.equals(motDePasse))
            return true;
        else
        	return false;
    }
}
